package com.qd.aliyun.config;

import com.alicom.mns.tools.DefaultAlicomMessagePuller;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qd.aliyun.listener.AliYunSmsReportMsgListener;
import com.qd.system.service.msg.base.MsgReportService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

/**
 * 初始化阿里云短信配置
 *
 * @author sjk
 */
@Service
@Slf4j
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "aliyun.sms", name = "enable", havingValue = "true")
public class AliYunConfigService {
    private final AliYunSmsConfig aliYunSmsConfig;
    private final ObjectMapper objectMapper;
    private final MsgReportService msgReportService;

    @Bean(name = "aliyunSmsClient")
    public Client aliyunSmsClient() throws Exception {
        log.info("初始化阿里云短信配置");
        com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();

        Config smsConfig = new Config();
        smsConfig.setCredential(credentialClient);
        smsConfig.setEndpoint(aliYunSmsConfig.getEndpoint());
        smsConfig.setAccessKeyId(aliYunSmsConfig.getAccessKeyId());
        smsConfig.setAccessKeySecret(aliYunSmsConfig.getAccessKeySecret());
        // 设置最大空转连接数
        smsConfig.setMaxIdleConns(10);
        Client client = new Client(smsConfig);
        aliyunSmsReport();
        return client;
    }

    @SneakyThrows
    public void aliyunSmsReport() {
        DefaultAlicomMessagePuller puller = new DefaultAlicomMessagePuller();

        //设置异步线程池大小及任务队列的大小，还有无数据线程休眠时间
        puller.setConsumeMinThreadSize(3);
        puller.setConsumeMaxThreadSize(10);
        puller.setThreadQueueSize(2000);
        puller.setPullMsgThreadSize(1);
        //和服务端联调问题时开启,平时无需开启，消耗性能
        puller.openDebugLog(aliYunSmsConfig.isDebugLog());
        /*
         * TODO 将messageType和queueName替换成您需要的消息类型名称和对应的队列名称
         * 云通信产品下所有的回执消息类型:
         * 短信服务
         * 1:短信回执：SmsReport，
         * 2:短息上行：SmsUp
         * 3:国际短信回执：GlobeSmsReport
         *
         * 号码隐私保护服务
         * 1.呼叫发起时话单报告：SecretStartReport
         * 2.呼叫响铃时报告：SecretRingReport
         * 3.呼叫接听时报告：SecretPickUpReport
         * 4.呼叫结束后话单报告：SecretReport
         * 5.录音状态报告：SecretRecording
         * 6.录音ASR状态报告：SecretAsrReport
         * 7.短信内容报告：SecretSmsIntercept
         * 8.计费通话报告：SecretBillingCallReport
         * 9.计费短信报告：SecretBillingSmsReport
         * 10.异常号码状态推送：SecretExceptionPhoneReport
         * 11.放音录音状态报告：SecretRingRecording
         * 12.电商物流详情报告：SmartLogisticsReport
         * 13.号码管理信息：NumberManagementReport
         *
         * 语音服务
         * 1.呼叫记录消息：VoiceReport
         * 2.呼叫中间状态消息：VoiceCallReport
         * 3.录音记录消息：VoiceRecordReport
         * 4.实时ASR消息：VoiceRTASRReport
         * 5.融合通信呼叫记录消息：ArtcCdrReport
         * 6.融合通信呼叫中间状态：ArtcTempStatusReport
         */

        puller.startReceiveMsg(aliYunSmsConfig.getAccessKeyId(), aliYunSmsConfig.getAccessKeySecret(), "SmsReport", aliYunSmsConfig.getQueueName(), new AliYunSmsReportMsgListener(objectMapper,msgReportService));
    }
}
